package com.douqu.game.core.config.hegemony;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.config.goods.PropConfig;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: miracle.
 * Description: 争霸赛奖励设置
 * Date: 2018/6/27 0027 下午 4:39
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class HegemonyRankRewardConfig extends GameObject {

    /** 组id */
    public int groupId;

    /** 排名 */
    public int num;

    /** 奖励 */
    public GoodsData[] reward;

    /** 超级奖励 */
    public GoodsData[] superReward;

    @Override
    public boolean setVariable(String key, String value) {
        if ("reward".equals(key)){
            reward = LoadUtils.loadGoodDataToArray(key,value);
            return true;
        }else if ("superReward".equals(key)){
            superReward = LoadUtils.loadGoodDataToArray(key,value);
            return true;
        }else{
            return super.setVariable(key,value);
        }
    }

    @Override
    public boolean check() {
        for (GoodsData goodsData:reward){
            if (goodsData.value <= 0){
                LogUtils.error("HegemonyRankRewardConfig check reward propvalue <= 0 -> id:" + id + " goodsValue:" + goodsData.value);
                return false;
            }
            if (goodsData.type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE){
                if (DataFactory.getInstance().getGameObject(DataFactory.PROP_KEY,goodsData.id) == null){
                    LogUtils.error("HegemonyRankRewardConfig check reward propid don't exist -> id:" + id + " propValue:" + goodsData.id);
                    return  false;
                }
            }
            else if (goodsData.type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE){
                if (DataFactory.getInstance().getGameObject(DataFactory.ASSET_KEY ,goodsData.id) == null){
                    LogUtils.error("HegemonyRankRewardConfig check reward assetid don't exist -> id:" + id + " assetValue:" + goodsData.id);
                    return  false;
                }
            }
            else if (goodsData.type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_CARDS_VALUE){
                if (DataFactory.getInstance().getGameObject(DataFactory.CARD_KEY ,goodsData.id) == null){
                    LogUtils.error("HegemonyRankRewardConfig check reward cardid don't exist -> id:" + id + " cardValue:" + goodsData.id);
                    return  false;
                }
            }else{
                LogUtils.error("HegemonyRankRewardConfig check reward goodtype error -> id:" + id + " goodstype:" + goodsData.type);
                return false;
            }
        }

        for (GoodsData goodsData:superReward){
            if (goodsData.value <= 0){
                LogUtils.error("HegemonyRankRewardConfig check superReward propvalue <= 0 -> id:" + id + " goodsValue:" + goodsData.value);
                return false;
            }
            if (goodsData.type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE){
                if (DataFactory.getInstance().getGameObject(DataFactory.PROP_KEY,goodsData.id) == null){
                    LogUtils.error("HegemonyRankRewardConfig check superReward propid don't exist -> id:" + id + " propValue:" + goodsData.id);
                    return  false;
                }
            }
            else if (goodsData.type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE){
                if (DataFactory.getInstance().getGameObject(DataFactory.ASSET_KEY ,goodsData.id) == null){
                    LogUtils.error("HegemonyRankRewardConfig check superReward assetid don't exist -> id:" + id + " assetValue:" + goodsData.id);
                    return  false;
                }
            }
            else if (goodsData.type == SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_CARDS_VALUE){
                if (DataFactory.getInstance().getGameObject(DataFactory.CARD_KEY ,goodsData.id) == null){
                    LogUtils.error("HegemonyRankRewardConfig check superReward cardid don't exist -> id:" + id + " cardValue:" + goodsData.id);
                    return  false;
                }
            }else{
                LogUtils.error("HegemonyRankRewardConfig check superReward goodtype error -> id:" + id + " goodstype:" + goodsData.type);
                return false;
            }
        }
        return true;
    }

    public static List<HegemonyRankRewardConfig> getRewardConfigList(int groupId){
        List<HegemonyRankRewardConfig> list = DataFactory.getInstance().getDataList(DataFactory.HEGEMONY_RANK_REWARD);
        List<HegemonyRankRewardConfig> rewardConfigList = new ArrayList<>();
        for (HegemonyRankRewardConfig config:list){
            if (config.groupId == groupId){
                rewardConfigList.add(config);
            }
        }
        return rewardConfigList;
    }
}
